/*
 * Created on Feb 26, 2005
 */
package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderReceiptForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.utils.Logger;


/**
 * @author Ravikanth
**/
public class PurchaseOrderReceiptSaveCommand extends ChipTrackExtendedAction
{
	/* 
	 * Purchase Order Receipt Save Command
	 */
	public ActionForward executeRequest( ActionMapping mapping,	ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		HashMap errors			= new HashMap();
		HashMap info            = new HashMap();
		String actionForward 	= "receiptSearch";
		String action   		= request.getParameter( REQUEST_ACTION );
		HttpSession mSession 	= request.getSession();
		PurchaseOrderReceiptForm purchaseOrderReceiptForm = ( PurchaseOrderReceiptForm ) form;
		User user = (User)mSession.getAttribute( SessionKeys.USER_KEY );

		try
		{
            if ( isTokenValid ( request ) )
            {
                if( action.equalsIgnoreCase( "save" ) )
                {
                    // Check wether the PO Number already Exists
                    if( purchaseOrderReceiptForm.getReceipt_id() == -1 && PurchaseOrderDAO.isReceiptNumberExists( purchaseOrderReceiptForm.getReceipt_Number() ) )
                    {
                        purchaseOrderReceiptForm.setReceipt_Number( PurchaseOrderDAO.loadAutomatedReceiptNo() );
                        info.put( "Receipt Number already exists. New Receipt number has been generated ", purchaseOrderReceiptForm.getReceipt_Number() );
                        actionForward = "receipt";
                    }
                    else
                    {
                        // Save the Purchase Order Receipt
                        PurchaseOrderDAO.savePurchaseOrderReceipt( purchaseOrderReceiptForm, user.getUserId() );
                        resetToken ( request );

                        actionForward = "saveSuccess";
                        Logger.info( INFO_PURCHASE_ORDER_RECEIPT_SAVED_SUCCEFULLY );
                        info.put( purchaseOrderReceiptForm.getReceipt_Number(), INFO_PURCHASE_ORDER_RECEIPT_SAVED_SUCCEFULLY );
                        info.put( "View/Print ", "Click here to <a href='javascript:showPoForViewPrint("+purchaseOrderReceiptForm.getReceipt_id()+");' onmouseover=\"window.status='Click here to View/print purchase order receipt';return true\" onmouseout=\"window.status='';return true\"> view/print </a>Purchase Order Receipt Details");

                        mSession.removeAttribute( "purchaseOrderReceiptForm" );
                    }
                }
            }
            else
            {
                errors.put ( ERROR_PURCHASE_ORDER_RECEIPT_ALREADY_SAVED, null );
                actionForward = "saveSuccess";
            }
            request.setAttribute( "vendorsList", PurchaseOrderDAO.loadAllVendors() );
            request.setAttribute( "yearList", PurchaseOrderDAO.getYearList () );
            request.setAttribute( "productList", PurchaseOrderDAO.loadPOProductsList() );
		}
		catch ( Exception e )
		{
            e.printStackTrace();
			Logger.error( ERROR_SAVING_RECEIVINGS_TRANSACTION_INFORMATION, e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, "" );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( ERROR_SAVING_RECEIVINGS_TRANSACTION_INFORMATION, e ) );
			actionForward = "receiptSearch";
			request.setAttribute( "vendorsList", PurchaseOrderDAO.loadAllVendors() );
			request.setAttribute( "yearList", PurchaseOrderDAO.getYearList () );
			request.setAttribute( "productList", PurchaseOrderDAO.loadPOProductsList() );
		}
        
		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		
		return mapping.findForward( actionForward );
	}
}
